//
//  checkViewController.m
//  dddddemo
//
//  Created by lanou3g on 15/11/5.
//  Copyright © 2015年 zhk. All rights reserved.
//

#import "checkViewController.h"
#import "DataSqlite.h"
#import "ModelFirst.h"
#import "checkView.h"
#import <AVFoundation/AVFoundation.h>
#import "ClickImage.h"
#import "EditViewController.h"

@interface checkViewController ()<UIAlertViewDelegate>
@property(nonatomic,strong)ModelFirst * model;
@property(nonatomic,strong)checkView * ckV;
@property(nonatomic,strong)AVAudioPlayer * avpl;
@property(nonatomic,strong)EditViewController * ed;
@end

@implementation checkViewController

-(void)loadView
{
    self.ckV = [[checkView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.ckV;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.ckV.backgroundColor = [UIColor colorWithRed:255 / 255.0 green: 211 / 255.0 blue: 155 / 255.0 alpha:1.0];


    self.model = [[DataSqlite shareDataSqlite] findModelFirstByTitle:self.title];
    
    self.ckV.LabelTitle.text = self.model.title;
    self.ckV.LabelTitle.textAlignment = NSTextAlignmentCenter;
    self.ckV.LabelTitle.numberOfLines = 0;

    self.ckV.LabelTitle.font = [UIFont systemFontOfSize:19.0f];
    CGFloat tempTitleHeight = [self heightForTitle:self.model.title];
    CGRect tempTitleRect = self.ckV.LabelTitle.frame;
    tempTitleRect.size.height = tempTitleHeight;
    self.ckV.LabelTitle.frame = tempTitleRect;

    self.ckV.LabelDetai.text = self.model.content;
    self.ckV.LabelDetai.textAlignment = NSTextAlignmentRight;
    CGFloat tempY = tempTitleHeight ;
    CGRect tempRect = self.ckV.LabelDetai.frame;
    tempRect.origin.y = tempY + 60;
    self.ckV.LabelDetai.frame = tempRect;
    self.ckV.LabelDetai.numberOfLines = 0;
    self.ckV.LabelDetai.font = [UIFont systemFontOfSize:15.0];
    CGFloat tempDetailHeight = [self heightForDetail:self.model.content];
    CGRect tempDetailRect = self.ckV.LabelDetai.frame;
    tempDetailRect.size.height = tempDetailHeight;
    self.ckV.LabelDetai.frame = tempDetailRect;

    CGFloat imageY = tempDetailHeight + tempTitleHeight;
    CGRect imageRect = self.ckV.imageView.frame;
    imageRect.origin.y = imageY + 80;
    CGRect imageTwoRect = self.ckV.imageViewTwo.frame;
    imageTwoRect.origin.y = imageY + 80;
    self.ckV.imageViewTwo.frame = imageTwoRect;
    self.ckV.imageView.frame = imageRect;
    CGRect imageThreeRect = self.ckV.imageViewThree.frame;
    imageThreeRect.origin.y = imageY + 80;
    self.ckV.imageViewThree.frame = imageThreeRect;
    
    self.ckV.imageView.image = [UIImage imageWithData:self.model.imageData];
    self.ckV.imageViewTwo.image = [UIImage imageWithData:self.model.imageData2];
    self.ckV.imageViewThree.image = [UIImage imageWithData:self.model.imageData3];
    
    
    CGRect rec = self.ckV.imageView.frame;
    CGFloat buttonY = rec.origin.y + 150;
    CGRect te = self.ckV.luyinBtn.frame;
    te.origin.y = buttonY;
    self.ckV.luyinBtn.frame = te;
    
    
    self.ckV.contentSize = CGSizeMake(self.view.frame.size.width ,60 + tempDetailHeight + tempTitleHeight + self.ckV.imageView.frame.size.height + 60 + 60);
    
    [self.ckV.luyinBtn addTarget:self action:@selector(talkAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightAction)];
    
    NSString * documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    
    NSString * sss = [documentsPath stringByAppendingString:[NSString stringWithFormat:@"/Sound/%@",self.model.ReFilePath]];
    
    NSURL * url = [NSURL URLWithString:sss];
    

    self.avpl = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    
    
    
    // 添加手势
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTapFirst:)];
    
    [self.ckV.imageView addGestureRecognizer:tap];
    
    tap.numberOfTapsRequired = 1;
    
    self.ckV.imageView.userInteractionEnabled = YES;
    
    
    
    UITapGestureRecognizer * tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTapSecond:)];
    
    [self.ckV.imageViewTwo addGestureRecognizer:tap2];
    
    tap2.numberOfTapsRequired = 1;
    
    self.ckV.imageViewTwo.userInteractionEnabled = YES;
    
    UITapGestureRecognizer * tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTapThird:)];
    
    [self.ckV.imageViewThree addGestureRecognizer:tap3];
    
    tap3.numberOfTapsRequired = 1;
    
    self.ckV.imageViewThree.userInteractionEnabled = YES;

    
}

-(void)viewWillAppear:(BOOL)animated{
    if(self.model.ReFilePath.length == 0){
        self.ckV.luyinBtn.hidden = YES;
    }else{
        self.ckV.luyinBtn.hidden = NO;
    }
}

- (void)imageTapFirst:(UIImage *)image
{
    if (self.ckV.imageView.image != nil) {
        [ClickImage showImage:(UIImageView *)self.ckV.imageView];
    }
}

- (void)imageTapSecond:(UIImage *)image
{
    if (self.ckV.imageViewTwo.image != nil) {
        [ClickImage showImage:(UIImageView *)self.ckV.imageViewTwo];
    }
}

- (void)imageTapThird:(UIImage *)image
{
    if (self.ckV.imageViewThree.image != nil) {
        [ClickImage showImage:(UIImageView *)self.ckV.imageViewThree];
    }
}

- (void)talkAction
{
    [self.avpl play];
}


- (void)rightAction{
    
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"真的要删除吗?" message:@"每一段记录都是一段回忆，删了可就再也找不回来了，想好了吗？" delegate:self cancelButtonTitle:@"想好了" otherButtonTitles:@"不删了.", nil];
    
    [alert show];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [[DataSqlite shareDataSqlite] deleteModelByTitle:self.title];
        
        NSString * documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        
        NSString * sss = [documentsPath stringByAppendingString:[NSString stringWithFormat:@"/Sound/%@",self.model.ReFilePath]];
        
        [[NSFileManager defaultManager] removeItemAtPath:sss error:nil];
        
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else
    {
        return;
    }
}

- (CGFloat)heightForTitle:(NSString *)aString
{
    NSDictionary * dict = @{NSFontAttributeName:[UIFont systemFontOfSize:19.0f]};
    CGRect rect = [aString boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.ckV.frame) - 40, 20000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    
    return rect.size.height;
}

- (CGFloat)heightForDetail:(NSString *)aString
{
    NSDictionary * dict = @{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]};
    CGRect rect = [aString boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.ckV.frame) - 20, 20000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    return rect.size.height;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
